package com.study.example.gameserver.base.dao.convert;


import com.sparrow.common.map.Position;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter(autoApply = true)
public class PositionConverter implements AttributeConverter<Position, String> {

    @Override
    public String convertToDatabaseColumn(Position position) {
        if (position == null) {
            return null;
        }
        // 这里可以用 JSON / 二进制 / 压缩方式
        return position.getX() + "," + position.getY() + "," + position.getZ();
    }

    @Override
    public Position convertToEntityAttribute(String dbData) {
        if (dbData == null || dbData.isEmpty()) {
            return null;
        }
        String[] parts = dbData.split(",");
        return new Position(
                Float.parseFloat(parts[0]),
                Float.parseFloat(parts[1]),
                Float.parseFloat(parts[2])
        );
    }
}

